package com.mysiteforme.admin.controller.admin;

import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.ElectricityClassify;
import com.mysiteforme.admin.service.ElectricityClassifyService;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分类  前端控制器
 * </p>
 *
 * @author wangl
 * @since 2019-09-19
 */
@Controller
@RequestMapping("/admin/electricityClassify")
public class ElectricityClassifyController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElectricityClassifyController.class);

    @Autowired
    private ElectricityClassifyService electricityClassifyService;

    @GetMapping("list")
    @SysLog("跳转分类列表")
    public String list(){
        return "/admin/electricityClassify/test";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求分类列表数据")
    public LayerData<ElectricityClassify> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<ElectricityClassify> layerData = new LayerData<>();
        EntityWrapper<ElectricityClassify> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag",false);
        if(!map.isEmpty()){
            String name = (String) map.get("name");
            if(StringUtils.isNotBlank(name)) {
                wrapper.like("name",name);
            }else{
                map.remove("name");
            }

        }
        Page<ElectricityClassify> pageData = electricityClassifyService.selectPage(new Page<>(page,limit),wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @PostMapping("classifylist")
    @ResponseBody
    public RestResponse classifylist(){
        Map<String,Object> map = Maps.newHashMap();
        map.put("parentId",null);
        map.put("isShow",false);
        List list = electricityClassifyService.selectAllClassify(map);
        return RestResponse.success().setData(list);
    }

    @GetMapping("add")
    @SysLog("跳转新增分类页面")
    public String add(@RequestParam(value = "parentId",required = false) Long parentId,Model model){
        if(parentId != null){
            ElectricityClassify electricityClassify = electricityClassifyService.selectById(parentId);
            model.addAttribute("parentMenu",electricityClassify);
        }
        return "/admin/electricityClassify/add";
    }

    @PostMapping("add")
    @SysLog("保存新增分类数据")
    @ResponseBody
    public RestResponse add(ElectricityClassify electricityClassify){
        electricityClassifyService.insert(electricityClassify);
        return RestResponse.success();
    }

    @GetMapping("edit")
    @SysLog("跳转编辑分类页面")
    public String edit(Long id,Model model){
        ElectricityClassify electricityClassify = electricityClassifyService.selectById(id);
        model.addAttribute("electricityClassify",electricityClassify);
        return "/admin/electricityClassify/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑分类数据")
    public RestResponse edit(ElectricityClassify electricityClassify){
        if(null == electricityClassify.getId() || 0 == electricityClassify.getId()){
            return RestResponse.failure("ID不能为空");
        }
        electricityClassifyService.updateById(electricityClassify);
        return RestResponse.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除分类数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        ElectricityClassify electricityClassify = electricityClassifyService.selectById(id);
        electricityClassify.setDelFlag(true);
        electricityClassifyService.updateById(electricityClassify);
        return RestResponse.success();
    }

}
